from . import models
from . import serializers
from utils.get_query_params import GetQueryParams
from utils.commonviews import CommonAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from ProjectTool.ApiTool.parsing_data import ParsingData
from ProjectTool.common_tool.data_tool.file_uploaded import FileUploaded
from django.http.request import QueryDict
from django.core.files.uploadedfile import InMemoryUploadedFile


# 测试api
class TestApi(APIView):
    def get(self, request):
        # 查询数据库中的数据
        api_id = request.GET.get('id')
        queryset = models.Api.objects.filter(id=api_id).first()
        obj = ParsingData(serializers.ApiSerializers, queryset)
        obj.run()
        obj.res.data = obj.data_warehouse
        res = obj.res
        return Response(res.dict)


# 测试接口明细
class TestApiDetail(APIView):
    def get(self, request):
        # 查询数据库中的数据
        api_detail_id = request.GET.get('id')
        queryset = models.ApiDetail.objects.filter(id=api_detail_id).first()
        obj = ParsingData(serializers.ApiDetailSerializers, queryset)
        obj.run()
        obj.res.data = obj.data_warehouse
        res = obj.res
        return Response(res.dict)


class ApiModelViewSet(CommonAPIView):
    query_set = models.Api.objects.all().order_by('id')
    serializer_class = serializers.ApiSerializers
    input_get_list = [('api_id', 'id'), ('api_name', 'name__contains'), ('inputObjName', 'name__contains')]
    input_put_list = [('api_id', 'id')]
    input_delete_list = [('api_id', 'id')]
    input_dict = GetQueryParams

    # def post(self, request):
    #     fields = ['request_header', 'request_body', 'response_data', 'request_push', 'request_pull', 'assert_push',
    #               'assert_pull']
    #     data = request.data
    #     if isinstance(data, QueryDict):
    #         _mutable = data._mutable
    #         data._mutable = True
    #         for key in data:
    #             if key in fields and not isinstance(data[key], InMemoryUploadedFile):
    #                 obj = FileUploaded(data[key], ''.join([key, '.txt'])).get_uploaded_file()
    #                 data[key] = obj
    #         data._mutable = _mutable
    #     return self.create(request)
    #
    # def put(self, request):
    #     fields = ['request_header', 'request_body', 'response_data', 'request_push', 'request_pull', 'assert_push',
    #               'assert_pull']
    #     data = request.data
    #     if isinstance(data, QueryDict):
    #         _mutable = data._mutable
    #         data._mutable = True
    #         for key in data:
    #             if key in fields and not isinstance(data[key], InMemoryUploadedFile):
    #                 obj = FileUploaded(data[key], ''.join([key, '.txt'])).get_uploaded_file()
    #                 data[key] = obj
    #         data._mutable = _mutable
    #     return self.update(request)


class ApiDetailModelViewSet(CommonAPIView):
    query_set = models.ApiDetail.objects.all().order_by('id')
    serializer_class = serializers.ApiDetailSerializers
    input_get_list = [('apiDetail_id', 'id'), ('api_id', 'api_id'), ('apiDetail_name', 'title__contains'),
                      ('inputObjName', 'title__contains')]
    input_put_list = [('apiDetail_id', 'id')]
    input_delete_list = [('apiDetail_id', 'id')]
    input_dict = GetQueryParams

    # def post(self, request):
    #     fields = ['body', 'response', 'assert_data', 'result_data']
    #     data = request.data
    #     if isinstance(data, QueryDict):
    #         _mutable = data._mutable
    #         data._mutable = True
    #         for key in data:
    #             if key in fields and not isinstance(data[key], InMemoryUploadedFile):
    #                 obj = FileUploaded(data[key], ''.join([key, '.txt'])).get_uploaded_file()
    #                 data[key] = obj
    #         data._mutable = _mutable
    #     return self.create(request)
    #
    # def put(self, request):
    #     fields = ['body', 'response', 'assert_data', 'result_data']
    #     data = request.data
    #     print(data)
    #     if isinstance(data, QueryDict):
    #         _mutable = data._mutable
    #         data._mutable = True
    #         for key in data:
    #             if key in fields and not isinstance(data[key], InMemoryUploadedFile):
    #                 obj = FileUploaded(data[key], ''.join([key, '.txt'])).get_uploaded_file()
    #                 data[key] = obj
    #         data._mutable = _mutable
    #     print(data)
    #     return self.update(request)
